package com.hisan.freeride.home.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hisan.freeride.R;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.ConvertUtils;
import com.hisan.freeride.home.model.FReserve;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 首页行程列表适配器
 * Created by yu on 2017/1/17.
 */

public class ReserveAdapter extends RecyclerView.Adapter{
    private  List<FReserve> mList;
    private int type=1;
    private ButtonClickListener mButtonClickListener;
    private onItemClickListener mOnItemClickListener;
    public ReserveAdapter(int type, List<FReserve> list) {
        this.type=type;
        mList = list;
    }

    public void setNewData(int type, List<FReserve> list) {
        this.type=type;
        mList = list;
        notifyDataSetChanged();
    }

    public void setButtonClickListener(ButtonClickListener listener) {
        mButtonClickListener = listener;
    }
    public void setOnItemClickListener(onItemClickListener listener) {
        mOnItemClickListener = listener;
    }
    /**
     * 自定义的recyclerView点击监听
     */
    public interface onItemClickListener {
        void onItemClick(FReserve data);
    }
    /**
     * 按钮的点击监听
     */
    public interface ButtonClickListener {
        void onClick(int p,FReserve data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ReserveHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.reserve_list_item,null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        FReserve fReserve=mList.get(position);
        ReserveHolder ReserveHolder = (ReserveHolder) holder;
        ReserveHolder.total_name.setVisibility(View.GONE);
        ReserveHolder.status.setVisibility(View.VISIBLE);
        if (type>1){
            ReserveHolder.status.setVisibility(View.GONE);
        }
        if (!CollectionUtils.isNullOrEmpty(fReserve.getDriver_avatar())){
            ReserveHolder.logo.setImageURI(fReserve.getDriver_avatar());
        }
        ReserveHolder.logo.setOnClickListener(v -> {
            if (mButtonClickListener != null){
                //进入车主页面
                mButtonClickListener.onClick(4,fReserve);
            }
        });
        ReserveHolder.home.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(fReserve);
            }
        });
        ReserveHolder.plate.setText(fReserve.getCar_no()+"");
        ReserveHolder.name.setText(fReserve.getDriver_name());
        ReserveHolder.time.setText(fReserve.getDeparture_time());
        ReserveHolder.site.setText("出发地: "+fReserve.getInitially());
        if (!CollectionUtils.isNullOrEmpty(fReserve.getWay())){
            ReserveHolder.way.setText("途径地: "+fReserve.getWay());
        }else {
            ReserveHolder.way.setText("途径地: ");
        }
        ReserveHolder.address.setText("目的地: "+fReserve.getDestination());
        switch (fReserve.getOrder_state()){
            case 0:
                ReserveHolder.ename.setText("等待审核");
                ReserveHolder.status.setText("取消行程");
                break;
            case 1:
                ReserveHolder.ename.setText("通过审核");
                String time=fReserve.getDeparture_time()+":00";
                if (fReserve.getComment()==0&&getTime(time)>0){
                    ReserveHolder.status.setText("评价");
                    ReserveHolder.ename.setText("待评价");
                }else {
                    ReserveHolder.status.setText("取消行程");
                }
                break;
            case 2:
                ReserveHolder.ename.setText("车主拒绝");
                ReserveHolder.status.setVisibility(View.GONE);
                break;
            case 3:
                ReserveHolder.ename.setText("用户取消");
                ReserveHolder.status.setVisibility(View.GONE);
                break;
        }

        ReserveHolder.status.setOnClickListener(v -> {
            if (mButtonClickListener != null){
                String name=   ReserveHolder.status.getText().toString();
                int type=0;
                switch (name){
                    case "取消行程":
                        type=1;
                        //取消行程
                        mButtonClickListener.onClick(type,fReserve);
                        break;
                    case "评价":
                        //评价
                        type=2;
                        mButtonClickListener.onClick(type,fReserve);
                        break;
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList != null ? mList.size() : 0;
    }
    // 将字符串转为时间戳
    public  long getTime(String stime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate =new  Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        long st= ConvertUtils.timeDifference(stime,str);
        return st;
    }
}
